﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kaixin.Forms
{
    using Kaixin;
    using Kaixin.House.Garden;
    using Kaixin.House.Ranch;
    using Kaixin.Tools;

    public partial class frmMain : Form
    {
        private bool HadLogin = false;
        private bool _AutoMode = false;
        private bool _RanchAuto = false;

        public KaiXin001 ka = new KaiXin001();


        private delegate void PrintDelegate(string text);
        public frmMain()
        {
            InitializeComponent();
            //TextOutput.SetTextBox(this.tbResult);
            TextOutput.SetListBox(lbResult);
        }

        private void UpdateFriendsList()
        {
            try
            {
                clbFriends.DataSource = ka.FriendList.Items;
                clbFriends.DisplayMember = "Name";
                clbFriends.ValueMember = "Uid";
                clbFriends.SelectedIndexChanged += new EventHandler(clbFriends_SelectedIndexChanged);
            }
            catch (Exception)
            { 
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < clbFriends.Items.Count; i++)
                clbFriends.SetItemChecked(i, true);
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < clbFriends.Items.Count; i++)
                clbFriends.SetItemChecked(i, false);
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!HadLogin)
            {
                frmLogin frm = new frmLogin(this);
                frm.ShowDialog();
                if (frm.DialogResult == DialogResult.OK)
                {
                    HadLogin = true;
                    loginToolStripMenuItem.Text = "Logout";
                    UpdateFriendsList();
                    ka.GetRanch();
                    ka.AntiTheiveTriggered += new AntiTheiveTriggeredEvent(ka_AntiTheiveTriggered);
                }
            }
            else
            {
                clbFriends.SelectedIndexChanged -= clbFriends_SelectedIndexChanged;
                clbFriends.DataSource = null;
                HadLogin = false;
                loginToolStripMenuItem.Text = "Login...";
                ka.Logout();
                lvGarden.Items.Clear();
                TextOutput.Clear();
            }
        }

        private void GardenDoAction(Garden.ActionType a)
        {
            if (clbFriends.SelectedItem != null)
            {
                string fuid = (string)clbFriends.SelectedValue;
                ka.GetGarden(fuid).DoAction(a);
            }
        }

        private void ka_AntiTheiveTriggered(object sender, EventArgs e)
        { 
            if (sender != null)
            {
                AntiTheive anti = sender as AntiTheive;
                frmAntiTheive frm = new frmAntiTheive(anti);
                //frm.Owner = this;
                frm.DoAntiTheive();
            }
        }

        private void btnThieve_Click(object sender, EventArgs e)
        {
            GardenDoAction(Garden.ActionType.Harvest);

        }

        private void btnAntiGrass_Click(object sender, EventArgs e)
        {
            GardenDoAction(Garden.ActionType.AntiGrass);
        }

        private void btnWater_Click(object sender, EventArgs e)
        {
            GardenDoAction(Garden.ActionType.Water);
        }

        private void btnVermin_Click(object sender, EventArgs e)
        {
            GardenDoAction(Garden.ActionType.AntiVermin);
        }

        private void btnPlough_Click(object sender, EventArgs e)
        {
            GardenDoAction(Garden.ActionType.Plough);
        }

        private void btnFarmSeed_Click(object sender, EventArgs e)
        {
            /*
            string name = "";
            string fuid = "";
            if (Util.GetNameUId(clbFriends.SelectedItem.ToString(), out name, out fuid))
            {
                ka.FarmSeed(fuid);
            }
             * */
        }

        private void ShowGarden(Garden garden)
        {
            lvGarden.BeginUpdate();
            lvGarden.Items.Clear();
            for (int i = 0; i < garden.FarmList.Count; i++)
            {
                Farm farm = garden.FarmList[i];
                if (farm.Status == 1)
                {
                    ListViewItem item = new ListViewItem();
                    ListViewItem.ListViewSubItem subitem;

                    item.UseItemStyleForSubItems = false;

                    if (farm.CanHarvest())
                        item.ForeColor = Color.Red;

                    //item.Text = farm.Status.ToString();
                    item.Text = farm.FarmNum.ToString();
                    item.SubItems.Add(farm.Shared.ToString());
                    if (farm.RemainNum>0)
                        item.SubItems.Add(farm.FruitNum.ToString() + "/" + farm.RemainNum.ToString());
                    else
                        item.SubItems.Add(farm.FruitNum.ToString());

                    subitem = item.SubItems.Add(farm.Name);
                    if (farm.CanHarvest())
                        subitem.ForeColor = Color.Red;

                    subitem = item.SubItems.Add(farm.Water.ToString());
                    if (farm.CanWater())
                        subitem.ForeColor = Color.Blue;

                    subitem = item.SubItems.Add(farm.Grass.ToString());
                    if (farm.CanAntiGrass())
                        subitem.ForeColor = Color.Green;

                    subitem = item.SubItems.Add(farm.Vermin.ToString());
                    if (farm.CanVermin())
                        subitem.ForeColor = Color.Brown;

                    item.SubItems.Add(farm.Thieved.ToString());
                    item.SubItems.Add(farm.GetCropsStatus());
                    item.SubItems.Add(farm.Grow +"/"+farm.TotalGrow);
                    item.SubItems.Add(farm.GrowRemainTime.GetString());
                    item.SubItems.Add(farm.ThieveWaitTime.GetString());
                    lvGarden.Items.Add(item);
                }
            }
            lvGarden.EndUpdate();
        }

        private void ShowRanch(Ranch ranch)
        {
            lvAnimal.BeginUpdate();
            lvAnimal.Items.Clear();
            for (int i = 0; i < ranch.Animals.Count; i++)
            {
                Animal animal = ranch.Animals[i];
                ListViewItem item = new ListViewItem();

                if (animal.CanHarvest())
                    item.ForeColor = Color.Red;

                if (animal.CanProduct())
                    item.ForeColor = Color.Blue;

                if (animal.Hungry())
                    item.ForeColor = Color.Brown;

                item.Text = animal.Name;
                lvAnimal.Items.Add(item);
            }
            lvAnimal.EndUpdate();

            lvProduct.BeginUpdate();
            lvProduct.Items.Clear();
            for (int i = 0; i < ranch.Products.Count; i++)
            {
                Product product = ranch.Products[i];
                ListViewItem item = new ListViewItem();

                if (!product.Theived)
                    item.ForeColor = Color.Red;

                item.Text = product.Name;
                item.SubItems.Add((product.Num - product.StealNum) + "/" + product.Num);
                lvProduct.Items.Add(item);
                
            }
            lvProduct.EndUpdate();
        }

        private void farmAllToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            GardenDoAction(Garden.ActionType.FarmSeedRandom);
        }

        private void btnAuto_Click(object sender, EventArgs e)
        {
            /*
            while (true)
            {
                this.Invoke(new PrintDelegate(TextOutput.PrintLine), new object[] { "testing..." });
                Application.DoEvents();
            }
             * */

            if (_AutoMode)
            {
                _AutoMode = false; ;
                btnAuto.Text = "Auto";
                /*
                clbFriends.Enabled = true;
                lvGarden.Enabled = true;
                btnThieve.Enabled = true;
                btnWater.Enabled = true;
                btnAntiGrass.Enabled = true;
                btnVermin.Enabled = true;
                btnPlough.Enabled = true;
                menuStrip2.Enabled = true;
                 * */
                ka.StopGardenAuto();
            }
            else
            {
                _AutoMode = true;
                btnAuto.Text = "Stop";
                /*
                clbFriends.Enabled = false;
                lvGarden.Enabled = false;
                btnThieve.Enabled = false;
                btnWater.Enabled = false;
                btnAntiGrass.Enabled = false;
                btnVermin.Enabled = false;
                btnPlough.Enabled = false;
                menuStrip2.Enabled = false;
                */
                ka.GardenDoAuto();
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void clbFriends_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (clbFriends.SelectedItem != null)
            {
                string fuid = (string)clbFriends.SelectedValue;

                switch (tcHouse.SelectedIndex)
                {
                    case 1:
                        Garden garden = ka.GetGarden(fuid);
                        this.ShowGarden(garden);
                        break;
                    case 2:
                        Ranch ranch = ka.GetRanch(fuid);
                        this.ShowRanch(ranch);
                        break;
                }
            }
        }

        private void btnRanchAuto_Click(object sender, EventArgs e)
        {
            if (_RanchAuto)
            {
                _RanchAuto = false;
                btnRanchAuto.Text = "Auto";
                ka.StopRanchAuto();
            }
            else
            {
                _RanchAuto = true;
                btnRanchAuto.Text = "Stop";
                ka.RanchDoAuto();
            }
        }

        private void RanchDoAction(Ranch.ActionType a)
        {
            if (clbFriends.SelectedItem != null)
            {
                string fuid = (string)clbFriends.SelectedValue;
                ka.GetRanch(fuid).DoAction(a);
            }
        }

        private void btnRWater_Click(object sender, EventArgs e)
        {
            RanchDoAction(Ranch.ActionType.Water);
        }

        private void btnRGrass_Click(object sender, EventArgs e)
        {
            //RanchDoAction(Ranch.ActionType.Grass);

            if (clbFriends.SelectedItem != null)
            {
                frmInputNumber frm = new frmInputNumber();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    string fuid = (string)clbFriends.SelectedValue;
                    ka.GetRanch(fuid).DoGrass(frm.GetValue());
                }
            }
        }

        private void btnRCarrot_Click(object sender, EventArgs e)
        {
            RanchDoAction(Ranch.ActionType.Carrot);
        }

        private void btnRHarvestP_Click(object sender, EventArgs e)
        {
            RanchDoAction(Ranch.ActionType.HarvestProduct);
        }

        private void btnRHarvestA_Click(object sender, EventArgs e)
        {
            RanchDoAction(Ranch.ActionType.HarvestAnimal);
        }

        private void btnRProduct_Click(object sender, EventArgs e)
        {
            RanchDoAction(Ranch.ActionType.ProductAnimal);
        }


    }
}
